package com.blockmeta.bbs.businesslibrary.net.interceptor;


import android.text.TextUtils;

import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.businesslibrary.util.DeviceUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by author on 2018/8/13.
 * 为HttpClient插入头部数据
 */

public class OAuthInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        String cacheControl = originalRequest.cacheControl().toString();
        String uid = "";
        if (AccountHelper.getUserPhonePOJO() != null && AccountHelper.getUid() != null
                && !TextUtils.isEmpty(AccountHelper.getUid())) {
            uid = AccountHelper.getUid();
        }
        Request.Builder requestBuilder = originalRequest.newBuilder()
                .header("uid", uid)
                .addHeader("from", "android")
                .addHeader("device_id", DeviceUtil.getIMEI())
                .method(originalRequest.method(), originalRequest.body());

        Request request = requestBuilder.build();

        Response originalResponse = chain.proceed(request);
        Response.Builder responseBuilder =
                //Cache control设置缓存
                originalResponse.newBuilder().header("Cache-Control", cacheControl);

        return responseBuilder.build();
    }
}
